﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Base.Core.UnityFactory;
using BF.IBLL.Dictionary;
using BF.Infrastructure;
using System.Web.UI.HtmlControls;
using BF.Model;

namespace BF.Web
{
    public partial class ChildList : BasePage
    {
        Idic_DictBLL bll = IOCFactory.GetIOCResolve<Idic_DictBLL>();//BLL

        #region 系统事件

        protected void lbtnStop_Click(object sender, EventArgs e)                       //停用
        {
            int iflag = int.Parse(hideStop.Value);
            for (int i = 0; i < rptDict.Items.Count; i++)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)rptDict.Items[i].FindControl("cbxGKey");
                if (chk != null && chk.Checked)
                {
                    dic_Dict tempModel = bll.GetModel(new Guid(chk.Value));
                    tempModel.iUse = iflag;
                }
            }

            if (bll.Update())
            {
                BF.Web.App_Code.UICommon.UpdateDictList();       //更新内存中的标准字典表
                Messages(iflag == 1 ? "启用成功！" : "停用成功！");
            }
            else
            {
                Messages(iflag == 1 ? "启用失败！" : "停用失败！");
            }

            anpDict.CurrentPageIndex = 1;
        }

        protected void anpDict_PageChanged(object sender, EventArgs e)                  //翻页事件
        {
            GetValue(anpDict.PageSize, anpDict.CurrentPageIndex);
        }
        #endregion

        #region 自定义方法
        protected void GetValue(int pageSize, int currentPageIndex)                     //初始化数据
        {
            //获取父类ID
            string parentCode = bll.GetModel(new Guid(GKey)).sCode;
            if (string.IsNullOrEmpty(parentCode))
                return;

            //获取明细总数
            anpDict.RecordCount = bll.GetRowCount(p =>//设置总数
            (string.IsNullOrEmpty(tbxSearch.Text.Trim()) ? true : p.sCode.Contains(tbxSearch.Text.Trim())) &&//用户查询条件：表编码(模糊查询)
            (string.IsNullOrEmpty(txtName.Text.Trim()) ? true : p.sName.Contains(txtName.Text.Trim())) &&
             p.sTableCode == parentCode
             );

            //用户查询条件：表编码(模糊查询)
            var modelList = bll.GetList(p =>
            (string.IsNullOrEmpty(tbxSearch.Text.Trim()) ? true : p.sCode.Contains(tbxSearch.Text.Trim())) &&//用户查询条件：表编码(模糊查询)
            (string.IsNullOrEmpty(txtName.Text.Trim()) ? true : p.sName.Contains(txtName.Text.Trim())) &&
             p.sTableCode == parentCode,
                p => new { p.iSort },
                new PageModel(currentPageIndex),
                true);

            rptDict.DataSource = modelList;//数据绑定
            rptDict.DataBind();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            hideGkey.Value = GKey;
            if (!IsPostBack)
            {
                anpDict.CurrentPageIndex = 1;
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)//查询
        {
            anpDict.CurrentPageIndex = 1;
        }

        protected void lbtnUp_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < rptDict.Items.Count; i++)
            {
                HtmlInputCheckBox cb = (HtmlInputCheckBox)rptDict
                    .Items[i].FindControl("cbxGKey");
                if (cb != null && cb.Checked)
                {
                    int iSort;// int.Parse(cb.Attributes["isort"]);
                    if (int.TryParse(cb.Attributes["isort"], out iSort))
                    {
                        iSort = int.Parse(cb.Attributes["isort"]);
                    }
                    bll.Up(cb.Value, iSort, GKey);
                    this.anpDict.CurrentPageIndex = 1;
                    return;
                }
            }

        }

        protected void lbtnDown_Click(object sender, EventArgs e)
        {
            List<string> delList = new List<string>();
            for (int i = 0; i < rptDict.Items.Count; i++)
            {
                HtmlInputCheckBox cb = (HtmlInputCheckBox)rptDict
                    .Items[i].FindControl("cbxGKey");
                if (cb != null && cb.Checked)
                {
                    int iSort;// int.Parse(cb.Attributes["isort"]);
                    if (int.TryParse(cb.Attributes["isort"], out iSort))
                    {
                        iSort = int.Parse(cb.Attributes["isort"]);
                    }
                    bll.Down(cb.Value, iSort, GKey);
                    this.anpDict.CurrentPageIndex = 1;
                    return;
                }
            }
        }

        #region 属性

        /// <summary>
        /// 修改数据的GUID
        /// </summary>
        public string GKey
        {
            get { return Request.QueryString["gKey"]; }
        }

        #endregion

        protected void lbtnSaveOrder_Click(object sender, EventArgs e)
        {
            string[] gKey = HFiSortgKey.Value.Split('^');
            string[] iSort = HFiSortValue.Value.Split('^');

            if (gKey != null && iSort != null && gKey.Length == iSort.Length)
            {
                if (bll.NewSortMethod(gKey, iSort))
                {
                    Messages("操作成功！");
                    //Messages("操作成功！", "function(){InitialBtnSort();}", this);
                    this.anpDict.CurrentPageIndex = 1;
                }
                else
                { Messages("操作失败！"); }
            }
        }

        public string GetDispalyStr(int param)
        {
            if (param == 1)
            {
                if (HFShowValue.Value == "none") { return "inline"; }
                else
                { return "none"; }
            }
            else
            {

                if (HFShowValue.Value == "none") { return "none"; }
                else
                { return "inline"; }
            }
        }
    }
}